/* ============================================================================
* 功能描述 ：按键函数实现
* 作者       ：ggls
* 创建日期 ：2024.5.22
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/


#include "main.h"





u16 pei_time = 0; 
u16 Distribution_time = 0; 


u32 led_time = 0;
u32 keysleep_time = 0;


bit unit_flag = 0;


unsigned int  uiKeyABTimeCnt12 = 0; 	//一起按下按键AB去抖动延时计数器
unsigned int  uiKeyATimeCnt12 = 0; 	//按下按键A去抖动延时计数器
unsigned int  double_Atime = 0; 	//按下按键A去抖动延时计数器
unsigned int  dvolume_settime = 0; 	//调节音量时间控制

char key_event = 0;

bit buzzer_on_off = 0;			//  报警功能开关 0 打开   1关闭
bit buzzer_mute = 0;
bit buzzer_buttonA = 0;
bit double_f = 0;
char LED_flag = 0;
char keysleep_flag = 0;

char double_A = 0;

char volume_set = 0;

char volume_stime = 0;


bit A_3  = 0;
bit A_6  = 0;
bit A_12  = 0;


bit S_3  = 0;
int yingao[3] = { 0x1290,0x1230,0x1070}; // 0x1230为60分贝	  0x1070为80分贝 	0x1290为40分贝

char yin_Dao = 0;


void Hly_Key_Init(void)
{
	/* 设置io功能 */
	GPIO_SET_MUX_MODE(P03CFG, GPIO_MUX_GPIO);		//设置P03为GPIO模式
	GPIO_ENABLE_INPUT(P0TRIS, GPIO_PIN_3);			//设置为输入模式
	GPIO_ENABLE_UP(P0UP, GPIO_PIN_3);				//开启下拉
	
	GPIO_SET_MUX_MODE(P01CFG, GPIO_MUX_GPIO);		//设置P01为GPIO模式
	GPIO_ENABLE_INPUT(P0TRIS, GPIO_PIN_1);			//设置为输入模式
	GPIO_ENABLE_UP(P0UP, GPIO_PIN_1);				//开启下拉


	/* 设置中断方式 */
	GPIO_SET_INT_MODE(P03EICFG, GPIO_INT_FALLING);	//设置为下降沿中断模式
	GPIO_EnableInt(GPIO0, GPIO_PIN_3_MSK);			//开启P03中断

	GPIO_SET_INT_MODE(P01EICFG, GPIO_INT_FALLING);	//设置为下降沿中断模式
	GPIO_EnableInt(GPIO0, GPIO_PIN_1_MSK);			//开启P01中断

//	P30 = 1;
	/*
	(3)设置中断优先级
	*/
	IRQ_SET_PRIORITY(IRQ_P0, IRQ_PRIORITY_HIGH);
	/*
	(4)开启总中断
	*/	
	IRQ_ALL_ENABLE();

	
}



void key_scan(void) 
{
	
	ms_counter++;
	if(ms_counter == 10000)
	{
		ms_counter = 0;

	}

	if( P03 == 0 && P01 == 0)
	{
		uiKeyABTimeCnt12++;
		if(uiKeyABTimeCnt12 >= 3000)  // 双键长按三秒
		{
			if(S_3 == 0)
			{
				key_event = 2;
			}
		}
	}
	else if( P01 == 0 && P03 == 1) // 单机A键
	{
		uiKeyATimeCnt12++;
		
		double_A  = 1;			// 单机A计时标志
		
		
		if(uiKeyATimeCnt12 >= 3000 && uiKeyATimeCnt12 < 6000)   // A键长按三秒
		{
			if(A_3 == 0)
			{
				key_event = 3;

			}
		}
		
		else if(uiKeyATimeCnt12 >= 12000)   // A键长按12秒
		{
			if(A_12 == 0)
			{
				_data2_tx_pack();
				key_event = 7;
			}
			key_event = 7;
			uiKeyATimeCnt12 = 0;

		}
		
		
	
		if(double_Atime < 300 && double_Atime > 1)		// 300 ms内双击
		{
			
			key_event = 6;				
			double_Atime = 0;
			double_f = 1;
			double_A  = 0;
			uiKeyATimeCnt12 = 0;
		}


	


	}
	else if( P01 == 1 && P03 == 0)
	{
		
	}
	else if( P01 == 1 && P03 == 1)
	{

		if(double_f == 1)
		{
			double_f = 0;
			double_A = 0;
			double_Atime = 0;

		}
		if(double_A == 1 )		// 单机A之后开始计时
		{
			double_Atime++;
		}

		if(double_Atime >= 300 )		// 超过300ms算作单机
		{
			
			if(uiKeyATimeCnt12 > 0 && uiKeyATimeCnt12 < 500)
			{
				uiKeyATimeCnt12 = 0;
				key_event = 5;
				double_Atime = 0;
				double_A  = 0;
				
			}
		}
		if(uiKeyATimeCnt12 >= 6000 && uiKeyATimeCnt12 < 12000)				// A键长按六秒
		{
			if(A_6 == 0)
			{
				key_event = 4;
				uiKeyATimeCnt12 = 0;
			}
		}
		
		if(uiKeyABTimeCnt12 > 0 && uiKeyABTimeCnt12 < 500)
		{
			uiKeyABTimeCnt12 = 0;
			key_event = 1;
		}
		if( A_3  == 1 || A_6 == 1 || A_12 == 1)
		{
			uiKeyATimeCnt12 = 0;

		}
	
		 A_3  = 0;
		 A_6  = 0;
		 A_12  = 0;
		
		
		 S_3  = 0;
	//	uiKeyATimeCnt12 = 0;
		uiKeyABTimeCnt12 = 0;
	}

	if(keysleep_flag == 1)
	{
		keysleep_time++;
		if(keysleep_time == 10000)
		{
			keysleep_flag = 0;
			keysleep_time = 0;
		}

	}
	if(LED_flag == 1)
	{
		led_time++;
		P00 = 1;
		if(led_time > 1000)
		{
			P00 = 0;
			led_time = 0;
			LED_flag = 0;
		}

	}


	if(peiwang_flag == 1)
	{
		pei_time++;
		if(pei_time >= 200)
		{
			P00 = ~P00;
			Distribution_time++;
			pei_time = 0;					
			
		}

		if(Distribution_time == 300)
		{
			P14 = 1;
			delayms(50);
			bl_out_Network_msg();
			delayms(50);
			P14 = 1;
			P00 = 0;
			Distribution_time = 0;
			peiwang_flag = 0;

		}
	}
}

void key_service(void)
{
	switch(key_event)
	{
		case 1:         // 双键单机
			key_event = 0;
	
			break;
		case 2:           // 双键三秒
			S_3 = 1;
			P14 = 1;
			delayms(150);	
			bl_Packet_Network_msg();
			delayms(50);	
			peiwang_flag = 1;
			key_event = 0;
			P14 = 1;
			break;
		case 3:							// A键三秒
			A_3 = 1;
			key_event = 0;
		//	buzzer_buttonA = 0; 
			break;
		case 4:						// A键六秒
			A_6 = 1;
			key_event = 0;
			buzzer_on_off = ~buzzer_on_off;

			if(buzzer_on_off == 1)
			{
				GPIO_SET_MUX_MODE(P13CFG,GPIO_MUX_BEEP);
				BUZ_EnableBuzzer(); 	
	
				BUZ_ConfigBuzzer(BUZ_CKS_32,111);			//Fsys=24Mhz，Fbuz = Fsys/(2*32*200) =2khz
				delayms(500);
				BUZ_ConfigBuzzer(BUZ_CKS_32,126);			//Fsys=24Mhz，Fbuz = Fsys/(2*32*200) =2khz
				delayms(500);
	
				BUZ_ConfigBuzzer(BUZ_CKS_32,143);			//Fsys=24Mhz，Fbuz = Fsys/(2*32*200) =2khz
				delayms(500);
	
				BUZ_DisableBuzzer();	


			}
			if(buzzer_on_off == 0)
			{
				GPIO_SET_MUX_MODE(P13CFG,GPIO_MUX_BEEP);
				BUZ_EnableBuzzer(); 	
				BUZ_ConfigBuzzer(BUZ_CKS_32,143);			//Fsys=24Mhz，Fbuz = Fsys/(2*32*200) =2khz
				delayms(500);
				
				BUZ_ConfigBuzzer(BUZ_CKS_32,126);			//Fsys=24Mhz，Fbuz = Fsys/(2*32*200) =2khz
				delayms(500);
	
				BUZ_ConfigBuzzer(BUZ_CKS_32,111);			//Fsys=24Mhz，Fbuz = Fsys/(2*32*200) =2khz
				delayms(500);
	
				BUZ_DisableBuzzer();	


			}
			break;
		case 5:						// A键单机
			key_event = 0;
			if(sound_adjustment == 1)
			{
				volume_set = 1;
			}
			if(key_door_f == 1)
			{
				Alarm_status = 1;

			}
	//	_data2_tx_pack();
			break;
		case 6:					// A键双击
			Alarm_status = 0;
			key_event = 0;
			
			break;
		case 7:					// A键十二秒
			key_event = 0;
			_data2_tx_pack();
			A_12 = 1;
			sound_adjustment = 1;
			break;
		
	
	}
}


void hly_volume_size_set(void)
{
	while(sound_adjustment)
	{
		key_service();

		if(volume_set == 1)
		{
			volume_set = 0;
			volume_stime = 0;

			yin_Dao++;
			if(yin_Dao == 3)
			{
				yin_Dao = 0;


			}
			if(yin_Dao == 2)
			{
				EPWM_ConfigChannelSymDuty(EPWM0, 0x1100);	 // 0x1430为60分贝        0x1100为80分贝  0x1490为40分贝

			}
			else if(yin_Dao == 0)
			{
				EPWM_ConfigChannelSymDuty(EPWM0, 0x1490);	 //0x1430为60分贝       0x1100为80分贝  0x1490为40分贝

			}
			else if(yin_Dao == 1)
			{
				EPWM_ConfigChannelSymDuty(EPWM0, 0x1430);	 // 0x1430为60分贝        0x1100为80分贝  0x1490为40分贝
			}


		}
		if(hly_get_elapsed_ms(dvolume_settime) >= 200)
		{
			dvolume_settime = get_current_ms();
			kaji_sb = ~kaji_sb;
			
			if(kaji_sb)
			{
				GPIO_SET_MUX_MODE(P13CFG, GPIO_MUX_PG0);
				EPWM_Start(EPWM_CH_0_MSK);
			}
			if(!kaji_sb)
			{
				GPIO_SET_MUX_MODE(P13CFG, GPIO_MUX_PG0);
				EPWM_Stop(EPWM_CH_0_MSK);
				volume_stime++;
			}
			
		}
		if(volume_stime >= 12)
		{
			sound_adjustment = 0;
			volume_stime = 0;
			EPWM_Stop(EPWM_CH_0_MSK);


		}


	}



}


